import _ from "lodash";

import Vue from "vue";
import VueRouter from "vue-router";

import { getTokenInfo } from "@utils/session";
import { queryParmKeyImport } from "@utils/router";

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}



Vue.use(VueRouter);

const routes = [
    {
        path: "/",
        redirect: "/home",
    },
    {
        path: "/home",
        name: "home",
        component: () =>
            import(/* webpackChunkName: "home" */ "@views/home/home.vue"),
        meta: {
            title: "首页",
        },
    },
    {
        path: "/login",
        name: "login",
        component: () =>
            import(/* webpackChunkName: "login" */ "@views/login/login.vue"),
        meta: {
            specialLayout: "fullScreen",
            noAuth: true,
            // keepAlive: true,
            title: "登录页",
        },
    },
    {
        path: "/register",
        name: "register",
        component: () =>
            import(/* webpackChunkName: "login" */ "@views/login/register.vue"),
        meta: {
            specialLayout: "fullScreen",
            noAuth: true,
            // keepAlive: true,
            title: "注册",
        },
    },
    {
        path: "/medicalVisit",
        name: "medicalVisit",
        component: () =>
            import(
                /* webpackChunkName: "medicalVisit" */ "@views/medicalVisit/medicalVisit.vue"
            ),
        meta: {
            title: "就诊查询",
        },
    },
    {
        path: "/userManage",
        name: "userManage",
        component: () =>
            import(
                /* webpackChunkName: "userManage" */ "@views/userManage/userManage.vue"
            ),
        meta: {
            title: "用户管理",
        },
    },
    {
        path: "/departManage",
        name: "departManage",
        component: () =>
            import(
                /* webpackChunkName: "departManage" */ "@views/departManage/departManage.vue"
            ),
        meta: {
            title: "科室管理",
        },
    },
    {
        path: "/diagnosisDict",
        name: "diagnosisDict",
        component: () =>
            import(
                /* webpackChunkName: "diagnosisDict" */ "@views/diagnosisDict/diagnosisDict.vue"
            ),
        meta: {
            title: "诊断字典",
        },
    },
    {
        path: "/orderItemDict",
        name: "orderItemDict",
        component: () =>
            import(
                /* webpackChunkName: "orderItemDict" */ "@views/orderItemDict/orderItemDict.vue"
            ),
        meta: {
            title: "医嘱字典",
        },
    },
    {
        path: "/patientManage",
        name: "patientManage",
        component: () =>
            import(
                /* webpackChunkName: "patientManage" */ "@views/patientManage/patientManage.vue"
            ),
        meta: {
            title: "患者档案",
        },
    },
    {
        path: "/visitManage",
        name: "visitManage",
        component: () =>
            import(
                /* webpackChunkName: "visitManage" */ "@views/visitManage/visitManage.vue"
            ),
        meta: {
            title: "就诊登记",
        },
    },
    {
        path: "/diagnosis",
        name: "diagnosis",
        component: () =>
            import(
                /* webpackChunkName: "diagnosis" */ "@views/diagnosis/diagnosis.vue"
            ),
        meta: {
            title: "医生接诊",
        },
    },
    {
        path: "/examList",
        name: "examList",
        component: () =>
            import(
                /* webpackChunkName: "examList" */ "@views/examList/examList.vue"
            ),
        meta: {
            title: "医技报告",
        },
    },
    {
        path: "/noExamList",
        name: "noExamList",
        component: () =>
            import(
                /* webpackChunkName: "noExamList" */ "@views/noExamList/noExamList.vue"
            ),
        meta: {
            title: "项目执行",
        },
    },
    {
        path: "/operlog",
        name: "operlog",
        component: () =>
            import(
                /* webpackChunkName: "operlog" */ "@views/operlog/operlog.vue"
            ),
        meta: {
            title: "操作日志",
        },
    },
    {
        path: "/roleManage",
        name: "roleManage",
        component: () =>
            import(
                /* webpackChunkName: "roleManage" */ "@views/roleManage/roleManage.vue"
            ),
        meta: {
            title: "角色管理",
        },
    },
    {
        path: "/iframe",
        name: "iframe",
        component: queryParmKeyImport("path", () =>
            import(/* webpackChunkName: "iframe" */ "@views/iframe/iframe.vue")
        ),
        meta: {
            title: "...",
            keepAlive: true,
        },
    },
    {
        path: "/404",
        name: "404",
        component: () =>
            import(/* webpackChunkName: "404" */ "@views/404/404.vue"),
        meta: {
            title: "未知页面",
            noAuth: true,
        },
    },
    {
        path: "*",
        name: "other",
        redirect: "/404",
    },
    {
        path: "/test",
        name: "test",
        component: () =>
            import(/* webpackChunkName: "test" */ "@views/test/testAsr.vue"),
        meta: {
            title: "test",
            noAuth: true,
        },
    },
];

const router = new VueRouter({
    mode: "hash",
    base: process.env.BASE_URL,
    routes,
});

router.beforeEach((to, from, next) => {
    console.log("router.beforeEach", to, from);
    let tokenInfo = getTokenInfo();

    if (!tokenInfo && (to.name !== "login")) {
        if (!_.get(to, "meta.noAuth")){
            next("/login");
            return;
        }
    }

    if (to.matched.length) {
        next();
    } else {
        next("/404");
    }
});

router.afterEach((to, from) => {
    // if (to.name === "login" && from.name !== "login" && !to.query.sourcePath) {
    //     let query = {
    //         ...to.query, 
    //         // sourcePath: from.fullPath 
    //     };
    //     router.replace({ name: "login", query });
    //     return;
    // }
    // 设置菜单
    // window.g_instance.$store.dispatch('account/setMenuByRoute', to);
});

export default router;
